﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NOAH.CMS.Repository;
using NOAH.CMS.Cache;

public class NewsController : Controller
{

    //[WhitespaceFilter]
    public ActionResult List(string id, string title, int? page = 1)
    {
        NewsCategory category = new NewsCategory();
        category = new NewsCategoryCacheLayer().GetAll().Find(p => p.ID.ToString() == id);
        ViewBag.NewsCategory = category;
        ViewBag.Title = category.Name;

        int pageSize = 24;
        int start = 0;
        int count = 0;

        start = (page.Value - 1) * pageSize;

        NewsArticleRepository repository = new NewsArticleRepository();
        List<NewsArticle> data = repository.GetByCategory(int.Parse(id), start, pageSize, out count);

        ///Set ViewBag
        ViewBag.PageIndex = page.Value;
        ViewBag.PageSize = pageSize;
        ViewBag.Count = count;
        return View(data);
    }

    //[WhitespaceFilter]
    public ActionResult Detail(string id, string cId, string title)
    {
        NewsArticleRepository repository = new NewsArticleRepository();
        NewsArticle detail = repository.Get(int.Parse(id));
        ViewBag.id = id;
        ViewBag.Title = title;
        ViewBag.cid = cId;


        NewsCategory category = new NewsCategory();
        category = new NewsCategoryCacheLayer().GetAll().Find(p => p.ID.ToString() == detail.CategoryID.Value.ToString());
        ViewBag.NewsCategory = category;

        return View(detail);
    }


    //public ActionResult _Home()
    //{
    //    List<NewsArticle> news = new NewsArticleRepository().GetNew(5);
    //    return View(news);
    //}

    //public ActionResult _Focus()
    //{
    //    List<NewsCategory> homeCates = new NewsCategoryCacheLayer().GetAll().FindAll(p => p.IsOnHomePage == true).OrderBy(p => p.Order).ToList();
    //    List<NewsListViewModel> data = new List<NewsListViewModel>();
    //    NewsArticleRepository repository = new NewsArticleRepository();

    //    foreach (NewsCategory c in homeCates)
    //    {
    //        NewsListViewModel nh = new NewsListViewModel();
    //        nh.NewsCategory = c;
    //        nh.News = repository.GetByCategory(c.ID, 5);
    //        data.Add(nh);
    //    }

    //    return View(data);
    //    //List<NewsArticle> news = new NewsArticleRepository().GetNew(5);
    //    //return View(news);
    //}
}
